package cn.rulian.one.log.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.Result;
import cn.rulian.one.log.bo.OpeLog;
import cn.rulian.one.log.dao.OpeLogDao;
import cn.rulian.one.log.dto.OpeLogModel;

/** 
 * Description: 操作日志
 */
@Service
@Transactional(readOnly = true)
public class OpeLogService
{

	private final Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private OpeLogDao dao;

	
	/**
	 * 查询
	 */
	public Result query(OpeLogModel m)
	{
		Sort sort = new Sort(Direction.DESC, "addTime"); //增加时间倒序
		Pageable pageable = new PageRequest(m.getPage()-1,m.getRows(),sort); //分页查询条件
		
		final String date_start = m.getDate_start();	//开始时间
		final String date_end = m.getDate_end();	//结束时间
		final String businessId = m.getBusinessId();	//业务ID
		final String type = m.getType();	//类型
		final String typeTwo = m.getTypeTwo();	//小类
		final String level = m.getLevel();	//级别
		final String msg = m.getMsg();	//日志
		 
		Specification<OpeLog> specification = new Specification<OpeLog>() 
		{
			@Override
            public Predicate toPredicate(Root<OpeLog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) 
			{
				 List<Predicate> predicates = new ArrayList<Predicate>();  
				 Date d=null;
				 
				 //开始时间
				 if(!xx.isEmpty(date_start))
				 {
					 d=xx.toDate(date_start);
					 predicates.add(cb.greaterThanOrEqualTo(root.<Date>get("addTime"),d));
				 }
				 
				 //结束时间
				 if(!xx.isEmpty(date_end))
				 {
					 d=xx.toTime(date_end+" 23:59:59");
					 predicates.add(cb.lessThanOrEqualTo(root.<Date>get("addTime"),d));
				 }
				 
				 //类型
				 if(!xx.isEmpty(type))
				 {
					 predicates.add(cb.like(root.<String>get("type"), "%"+type+"%"));
				 }
				 
				 //小类
				 if(!xx.isEmpty(typeTwo))
				 {
					 predicates.add(cb.like(root.<String>get("typeTwo"), "%"+typeTwo+"%"));
				 }
				 
				 //级别
				 if(!xx.isEmpty(level))
				 {
					 predicates.add(cb.like(root.<String>get("level"), "%"+level+"%"));
				 }

				 //业务ID
				 if(!xx.isEmpty(businessId))
				 {
					 predicates.add(cb.like(root.<String>get("businessId"), "%"+businessId+"%"));
				 }

				 //日志
				 if(!xx.isEmpty(msg))
				 {
					 predicates.add(cb.like(root.<String>get("msg"), "%"+msg+"%"));
				 }
				 
				return  cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
		
        
		
		Page<OpeLog> page =dao.findAll(specification, pageable);
		return Result.getResult(page);
	}
	
	/**
	 * 删除一批单据
	 */
	@Transactional
	public void delete(Long[] ids)
	{
		for(Long id:ids)
		{
			dao.delete(id);
		}
		log.info("删除操作日志。ids:{}",ids.toString());
		
	}
	
	public OpeLog findById(Long id)
	{
		return dao.findOne(id);
	}
	
}
